﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ywxt.NewsPush.Controllers
{
    using NHibernate;
    using NHibernate.Criterion;
    using NHibernate.Linq;
    using DB_NH;
    using ywxt.Common;
    using System.Net;
    using System.IO;
    using System.Web.Http;

    public class DoorController : Controller
    {
    [Authorize]
        public ActionResult Index()
        {
            var model = new Models.Doors.PageModelDoorIndex();

            #region 初始化门列表
            if (User.Identity.IsAuthenticated && User.Identity.IsAuthenticated)
            {
                //定义已验证的用户能够开启的门口
                Models.Doors.GXJDDoor[] _list = new Models.Doors.GXJDDoor[]
                {
                    new Models.Doors.GXJDDoor{
                        Name="开标一室",
                        Address=8,
                        ComPort="COM4",
                        ReaderNumber="04"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="开标二室",
                        Address=8,
                        ComPort="COM4",
                        ReaderNumber="03"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="开标三室",
                        Address=4,
                        ComPort="COM4",
                        ReaderNumber="04"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="评标一室",
                        Address=1,
                        ComPort="COM4",
                        ReaderNumber="02"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="评标二室",
                        Address=2,
                        ComPort="COM4",
                        ReaderNumber="03"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="评标三室",
                        Address=2,
                        ComPort="COM4",
                        ReaderNumber="04"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="开标四室",
                        Address=2,
                        ComPort="COM4",
                        ReaderNumber="02"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="开标五室",
                        Address=1,
                        ComPort="COM4",
                        ReaderNumber="04"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="文印室",
                        Address=4,
                        ComPort="COM4",
                        ReaderNumber="02"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="评标区",
                        Address=32,
                        ComPort="COM4",
                        ReaderNumber="02"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="大门",
                        ComPort="COM4",
                        Address=8,
                        ReaderNumber="02"
                    }
                };
                model.Doors.AddRange(_list);
            }

            if (User.Identity.IsAuthenticated && User.IsInRole("招标一部用户组"))
            {
                //定义角色能开的门
                Models.Doors.GXJDDoor[] _list = new Models.Doors.GXJDDoor[]
                {
                    new Models.Doors.GXJDDoor{
                        Name="招标一部",
                        Address=4,
                        ComPort="COM4",
                        ReaderNumber="01"
                    }
                };
                model.Doors.AddRange(_list);
            }

            if (User.Identity.IsAuthenticated && User.IsInRole("招标二部用户组"))
            {
                //定义角色能开的门
                Models.Doors.GXJDDoor[] _list = new Models.Doors.GXJDDoor[]
                {

                    new Models.Doors.GXJDDoor{
                        Name="招标二部",
                        Address=4,
                        ComPort="COM4",
                        ReaderNumber="03"
                    }
                };
                model.Doors.AddRange(_list);
            }

            if (User.Identity.IsAuthenticated && User.IsInRole("招标三部用户组"))
            {
                //定义角色能开的门
                Models.Doors.GXJDDoor[] _list = new Models.Doors.GXJDDoor[]
                {
                    new Models.Doors.GXJDDoor{
                        Name="招标三部",
                        Address=3,
                        ComPort="COM4",
                        ReaderNumber="04"
                    }
                };
                model.Doors.AddRange(_list);
            }

            if (User.Identity.IsAuthenticated && User.IsInRole("工程部用户组"))
            {
                //定义角色能开的门
                Models.Doors.GXJDDoor[] _list = new Models.Doors.GXJDDoor[]
                {
                    new Models.Doors.GXJDDoor{
                        Name="招标五部",
                        ComPort="COM4",
                        Address=3,
                        ReaderNumber="01"
                    }
                };
                model.Doors.AddRange(_list);
            }

            if (User.Identity.IsAuthenticated && User.IsInRole("工程部经理组"))
            {
                //定义角色能开的门
                Models.Doors.GXJDDoor[] _list = new Models.Doors.GXJDDoor[]
                {
                    new Models.Doors.GXJDDoor{
                        Name="招标五部经理",
                        ComPort="COM4",
                        Address=3,
                        ReaderNumber="02"
                    }
                };
                model.Doors.AddRange(_list);
            }

            if (User.Identity.IsAuthenticated && User.IsInRole("综合部用户组"))
            {
                //定义角色能开的门
                Models.Doors.GXJDDoor[] _list = new Models.Doors.GXJDDoor[]
                {
                    new Models.Doors.GXJDDoor{
                        Name="综合部",
                        ComPort="COM4",
                        Address=7,
                        ReaderNumber="02"
                    }
                };
                model.Doors.AddRange(_list);
            }

            if (User.Identity.IsAuthenticated && User.IsInRole("财务部用户组"))
            {
                //定义角色能开的门
                Models.Doors.GXJDDoor[] _list = new Models.Doors.GXJDDoor[]
                {
                    new Models.Doors.GXJDDoor{
                        Name="财务部",
                        ComPort="COM4",
                        Address=6,
                        ReaderNumber="01"
                    }
                };
                model.Doors.AddRange(_list);
            }

            if (User.Identity.IsAuthenticated && User.IsInRole("财务部经理组"))
            {
                //定义角色能开的门
                Models.Doors.GXJDDoor[] _list = new Models.Doors.GXJDDoor[]
                {
                    new Models.Doors.GXJDDoor{
                        Name="财务部经理",
                        ComPort="COM4",
                        Address=6,
                        ReaderNumber="03"
                    }
                };
                model.Doors.AddRange(_list);
            }

            if (User.Identity.IsAuthenticated && User.IsInRole("人力资源部"))
            {
                //定义角色能开的门
                Models.Doors.GXJDDoor[] _list = new Models.Doors.GXJDDoor[]
                {
                    new Models.Doors.GXJDDoor{
                        Name="人力资源部",
                        ComPort="COM4",
                        Address=5,
                        ReaderNumber="03"
                    }
                };
                model.Doors.AddRange(_list);
            }

            if (User.Identity.IsAuthenticated && User.IsInRole("质管部用户组"))
            {
                //定义角色能开的门
                Models.Doors.GXJDDoor[] _list = new Models.Doors.GXJDDoor[]
                {
                    new Models.Doors.GXJDDoor{
                        Name="质量部",
                        ComPort="COM4",
                        Address=7,
                        ReaderNumber="04"
                    }
                };
                model.Doors.AddRange(_list);
            }

            if (User.Identity.IsAuthenticated && User.IsInRole("总经理组"))
            {
                //定义角色能开的门
                Models.Doors.GXJDDoor[] _list = new Models.Doors.GXJDDoor[]
                {
                    new Models.Doors.GXJDDoor{
                        Name="总经理办公室",
                        ComPort="COM4",
                        Address=5,
                        ReaderNumber="01"
                    }
                };
                model.Doors.AddRange(_list);
            }

            if (User.Identity.IsAuthenticated && User.IsInRole("业务副总组"))
            {
                //定义角色能开的门
                Models.Doors.GXJDDoor[] _list = new Models.Doors.GXJDDoor[]
                {
                    new Models.Doors.GXJDDoor{
                        Name="业务副总办公室",
                        ComPort="COM4",
                        Address=35,
                        ReaderNumber="02"
                    }
                };
                model.Doors.AddRange(_list);
            }

            if (User.Identity.IsAuthenticated && User.IsInRole("综合副总组"))
            {
                //定义角色能开的门
                Models.Doors.GXJDDoor[] _list = new Models.Doors.GXJDDoor[]
                {
                    new Models.Doors.GXJDDoor{
                        Name="综合副总办公室",
                        ComPort="COM4",
                        Address=35,
                        ReaderNumber="01"
                    }
                };
                model.Doors.AddRange(_list);
            }

            if (User.Identity.IsAuthenticated && User.IsInRole("dept_xx"))
            {
                //定义角色能开的门
                Models.Doors.GXJDDoor[] _list = new Models.Doors.GXJDDoor[]
                {
                    new Models.Doors.GXJDDoor{
                        Name="招标一部",
                        Address=4,
                        ComPort="COM4",
                        ReaderNumber="01"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="招标二部",
                        Address=4,
                        ComPort="COM4",
                        ReaderNumber="03"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="招标三部",
                        Address=3,
                        ComPort="COM4",
                        ReaderNumber="04"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="招标五部",
                        ComPort="COM4",
                        Address=3,
                        ReaderNumber="01"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="招标五部经理",
                        ComPort="COM4",
                        Address=3,
                        ReaderNumber="02"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="综合副总办公室",
                        ComPort="COM4",
                        Address=35,
                        ReaderNumber="01"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="业务副总办公室",
                        ComPort="COM4",
                        Address=35,
                        ReaderNumber="02"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="综合部",
                        ComPort="COM4",
                        Address=7,
                        ReaderNumber="02"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="财务部",
                        ComPort="COM4",
                        Address=6,
                        ReaderNumber="01"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="财务部经理",
                        ComPort="COM4",
                        Address=6,
                        ReaderNumber="03"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="人力资源部",
                        ComPort="COM4",
                        Address=5,
                        ReaderNumber="03"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="质量部",
                        ComPort="COM4",
                        Address=7,
                        ReaderNumber="04"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="资料室",
                        ComPort="COM4",
                        Address=1,
                        ReaderNumber="03"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="大门",
                        ComPort="COM4",
                        Address=8,
                        ReaderNumber="02"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="询标室",
                        ComPort="COM4",
                        Address=8,
                        ReaderNumber="01"
                    }
                };
                model.Doors.AddRange(_list);
            }

            if (User.Identity.IsAuthenticated && User.IsInRole("jczh"))
            {
                //定义角色能开的门
                Models.Doors.GXJDDoor[] _list = new Models.Doors.GXJDDoor[]
                {
                    new Models.Doors.GXJDDoor{
                        Name="大门",
                        ComPort="COM4",
                        Address=8,
                        ReaderNumber="02"
                    },
                    new Models.Doors.GXJDDoor{
                        Name="精彩纵横办公室",
                        ComPort="COM4",
                        Address=7,
                        ReaderNumber="04"
                    }
                };
                model.Doors.AddRange(_list);
            }

            #endregion

            return View(model);
        }


        /// <summary>
        /// 根据上传的参数，开启指定门口
        /// </summary>
        /// <param name="_door"></param>
        /// <returns></returns>
        public JsonResult openDoor([FromBody]Models.Doors.GXJDDoor _door)
        {
            if (_door == null)
            {
                return Json(new CommonJsonResultForMVC("参数传递错误！"), JsonRequestBehavior.AllowGet);
            }

            CommonJsonResultForMVC _returnValue = new CommonJsonResultForMVC();

            var _command = new Models.Doors.CommandModel()
            {
                Address = _door.Address,
                ComPort = _door.ComPort,
                ReaderNumber = _door.ReaderNumber,
                UserName = User.Identity.Name
            };

            var _str_command = Server.UrlEncode(Common.EncodeBase64(Newtonsoft.Json.JsonConvert.SerializeObject(_command)));

            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://192.168.6.240:8080/door/?data=" + _str_command);
            req.Method = "GET";
            using (WebResponse wr = req.GetResponse())
            {
                StreamReader sr = new StreamReader(wr.GetResponseStream());
                var _str_res = sr.ReadToEnd();

                var _res = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.Doors.ResultModel>(_str_res);
                if (_res == null)
                {
                    _returnValue.ISOK = false;
                    _returnValue.MESSAGE = "返回数据错误！";
                }
                else if (_res != null && !_res.isok)
                {
                    _returnValue.ISOK = false;
                    _returnValue.MESSAGE = _res.message;
                }
            }

            return Json(_returnValue, JsonRequestBehavior.AllowGet);
        }

    }

}
